﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Media.Animation;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;

using ViewOnCore.Helper.WPF;

namespace UI.Control
{
    /// <summary>
    /// Interaction logic for Kiosk.xaml
    /// </summary>
    public partial class Kiosk : UserControl
    {

        #region Variables

        private ObservableCollection<AlbumCoverItem> _dataSource;
        private int _rows = -1;

        #endregion

        #region Constructor

        public Kiosk()
        {
            InitializeComponent();
            this.IsVisibleChanged += new DependencyPropertyChangedEventHandler(Kiosk_IsVisibleChanged);

            griMain.PreviewMouseLeftButtonDown += new MouseButtonEventHandler(griMain_PreviewMouseLeftButtonDown);
            griMain.PreviewMouseLeftButtonUp += new MouseButtonEventHandler(griMain_PreviewMouseLeftButtonUp);
            griMain.PreviewMouseMove += new MouseEventHandler(griMain_PreviewMouseMove);
            this.MouseDoubleClick += new MouseButtonEventHandler(Kiosk_MouseDoubleClick);
            this.SizeChanged += new SizeChangedEventHandler(Kiosk_SizeChanged);

            Cursor = Cursors.Hand;

            CalculateRows();
        }

        #endregion

        #region Properties

        public ObservableCollection<AlbumCoverItem> DataSource
        {
            set
            {
                _dataSource = value;
                _dataSource.CollectionChanged += new System.Collections.Specialized.NotifyCollectionChangedEventHandler(value_CollectionChanged);
            }
            get
            {
                return _dataSource;
            }
        }

        public int Rows
        {
            get
            {
                return _rows;
            }
        }

        public int ItemWidth
        {
            get
            {
                return 150;
            }
        }

        public int ItemHeight
        {
            get
            {
                return 150;
            }
        }

        #endregion

        #region Event : IsVisibleChanged

        void Kiosk_IsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            UpdateView();
        }

        #endregion

        #region Event : CollectionChanged

        void value_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            if (this.Visibility != Visibility.Visible)
                return;

            if (e.Action == NotifyCollectionChangedAction.Add)
                OnAddItems(e.NewItems);
            //else if (e.Action == NotifyCollectionChangedAction.Move)
            //    OnMoveItem(e.NewItems);
            else if (e.Action == NotifyCollectionChangedAction.Remove)
                OnRemoveItems(e.NewItems);
            //else if (e.Action == NotifyCollectionChangedAction.Replace)
            //    OnReplaceItem(e.NewItems);
            else if (e.Action == NotifyCollectionChangedAction.Reset)
                OnResetItem(e.NewItems);
            else
                throw new NotImplementedException();
        }

        #endregion

        #region Event : MouseDoubleClick

        void Kiosk_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            foreach (KioskRow row in stPanel.Children)
            {
                IInputElement ui = row.InputHitTest(e.GetPosition(row));
                if (ui != null)
                {
                    row.OnMouseDoubleClick(e);
                    return;
                }
            }
        }

        #endregion

        #region Event : OnMouseEnterItem / OnMouseLeaveItem

        internal void OnMouseEnterItem(AlbumCoverItem item)
        {
            txtLine1.Text = item.Title01;
            txtLine2.Text = item.Title02;
        }

        internal void OnMouseLeaveItem(AlbumCoverItem item)
        {
        }

        #endregion

        #region OnAddItems

        private void OnAddItems(IList items)
        {
            UpdateView();
        }

        #endregion

        #region OnRemoveItems

        private void OnRemoveItems(IList items)
        {
            UpdateView();
        }

        #endregion

        #region OnResetItem

        private void OnResetItem(IList items)
        {
            Dispatcher.BeginInvoke(DispatcherPriority.Normal,
                 (DelegateInvoker)delegate()
                 {
                     txtLine1.Text = "";
                     txtLine2.Text = "";

                     foreach (KioskRow row in stPanel.Children)
                         row.OnReset();

                     AsyncUpdateView();
                 });
        }

        #endregion

        #region UpdateView

        private void UpdateView()
        {
            Dispatcher.BeginInvoke(DispatcherPriority.Normal,
                (DelegateInvoker)delegate()
                {
                    AsyncUpdateView();
                });
        }

        private void AsyncUpdateView()
        {
            //---- Update the cover rows
            for (int row = stPanel.Children.Count; row < Rows; row++)
            {
                KioskRow kioskRow = new KioskRow(this, row);
                kioskRow.Height = ItemHeight;
                kioskRow.Width = this.Width;
                stPanel.Children.Add(kioskRow);
            }

            for (int index = stPanel.Children.Count - 1; index > Rows; index--)
                stPanel.Children.RemoveAt(index);

            //---- Update the rows
            foreach (KioskRow row in stPanel.Children)
                row.OnUpdate();
        }

        #endregion

        #region ReloadDataContext

        /// <summary>
        /// Reload all the datas
        /// </summary>
        public void ReloadDataContext()
        {
            if (this.Visibility == Visibility.Visible)
                AlbumCoverManager.Reload();
        }

        #endregion

        #region Animate mouse

        void griMain_PreviewMouseMove(object sender, MouseEventArgs e)
        {
            Canvas.SetLeft(eliMouse, e.GetPosition(canMouse).X - 12);
            Canvas.SetTop(eliMouse, e.GetPosition(canMouse).Y - 12);
        }

        void griMain_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            if (eliMouse.Opacity == 0)
                return;
            Storyboard animation = (Storyboard)eliMouse.FindResource("hideAnimation");
            animation.Begin(eliMouse);
        }

        void griMain_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (eliMouse.Opacity == 0.85)
                return;
            Storyboard animation = (Storyboard)eliMouse.FindResource("showAnimation");
            animation.Begin(eliMouse);
        }

        #endregion

        #region Event : SizeChanged

        void Kiosk_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            CalculateRows();
        }

        private void CalculateRows()
        {
            int rows = (int)(this.ActualHeight / ItemHeight);
            _rows = Math.Max(1, rows);
        }

        #endregion

        #region OnAlbumDialogOpening

        internal void OnAlbumDialogOpening()
        {
            griMain_PreviewMouseLeftButtonUp(null, null);
        }

        #endregion

    }
}
